#include "httplib.h"

void Hello(const httplib::Request &req, httplib::Response &rsp)
{
    rsp.set_content("welcome to my server", "text/plain");
    rsp.status = 200;
}

void Numbers(const httplib::Request &req, httplib::Response &rsp)
{
    auto num = req.matches[1]; //matches数组中，除了0下标为整体的path，往后的下标保存的是依次捕捉的数据

    rsp.set_content(num, "text/plain");
    rsp.status = 200;
}

void Multipart(const httplib::Request &req, httplib::Response &rsp)
{
    auto ret = req.has_file("file");
    if(ret == false) //不是文件
    {
        std::cout << "no file upload\n";
        rsp.status = 400;
        return;
    }
    const auto& file = req.get_file_value("file");

    rsp.body.clear(); //清空body内容
    rsp.body = file.filename; //文件名称
    rsp.body += "/n";
    rsp.body += file.content;//文件内容
    rsp.set_header("Content-Type", "text/plain");
    rsp.status = 200;
}

int main()
{
    httplib::Server server; //实例化Server类用于搭建服务器

    server.Get("/hi", Hello);
    server.Get(R"(/hi/(\d+))", Numbers);
    server.Post("/multipart", Multipart);

    server.listen("0.0.0.0", 8080); //任意网卡都可以接收来访，服务器端口8080

    return 0;
}